<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="题目的难度级别" prop="difficultyLevel" label-width="150px">
        <el-select v-model="queryParams.difficultyLevel" placeholder="请选择题目的难度级别" clearable>
          <el-option
            v-for="dict in question_difficulty"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="题目的类型" prop="questionType" label-width="100px">
        <el-select v-model="queryParams.questionType" placeholder="请选择题目的类型" clearable>
          <el-option
            v-for="dict in question_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="题目" prop="topic">
        <el-input
          v-model="queryParams.topic"
          placeholder="请输入题目"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:question:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:question:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:question:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:question:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" prop="id"  width="100px"/>
      <el-table-column label="题目简介" align="center" prop="topic" show-overflow-tooltip="true" />
      <!-- 题目的题干 -->
      <!-- <el-table-column label="题目" align="center" prop="questionText" /> -->
      <!-- <el-table-column label="题目的正确答案" align="center" prop="answer" /> -->
      <el-table-column label="题目难度" align="center" prop="difficultyLevel">
        <template #default="scope">
          <dict-tag :options="question_difficulty" :value="scope.row.difficultyLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="题目类型" align="center" prop="questionType">
        <template #default="scope">
          <dict-tag :options="question_type" :value="scope.row.questionType"/>
        </template>
      </el-table-column>
      <el-table-column label="题目题库" align="center" prop="questionBankId">
      <!-- getQuestionbankList -->
        <template #default="scope">
          <!-- 使用 v-for 遍历题库列表 -->
          <template v-for="item in questionbankList" :key="item.id">
            <span v-if="item.id === scope.row.questionBankId">{{ item.title }}</span>
          </template>
        </template>
      </el-table-column>
      <!-- <el-table-column label="出题人" align="center" prop="userId" /> -->
      <el-table-column label="创建时间" align="center" prop="creatTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="更新时间" align="center" prop="updataTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updataTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300px">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleselect(scope.row)" v-hasPermi="['manage:question:edit']">查看详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:question:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:question:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改题目展示对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="questionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="题目概述" prop="topic" label-width="100px">
          <el-input v-model="form.topic" placeholder="请输入题目" />
        </el-form-item>
        <el-form-item label="题目的题干" prop="questionText" label-width="100px">
          <el-input v-model="form.questionText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="题目的答案" prop="answer" label-width="100px">
          <el-input v-model="form.answer" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="题目的难度" prop="difficultyLevel" label-width="100px">
          <el-select v-model="form.difficultyLevel" placeholder="请选择题目的难度级别">
            <el-option
              v-for="dict in question_difficulty"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目的类型" prop="questionType" label-width="100px">
          <el-select v-model="form.questionType" placeholder="请选择题目的类型,1选择题2判断题3填空题4简答题">
            <el-option
              v-for="dict in question_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="题目的题库" prop="questionBankId" label-width="100px">
          <el-select v-model="form.questionBankId" placeholder="请选择题目的题库">
            <!-- <el-option
              v-for="item in questionbankList"
              :key="item
            /> -->
            <el-option
              v-for="item in questionbankList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        
      <!-- //   <el-table-column label="题目题库" align="center" prop="questionBankId">
      //   <template #default="scope">
      //     <template v-for="item in questionbankList" :key="item.id">
      //       <span v-if="item.id === scope.row.questionBankId">{{ item.title }}</span>
      //     </template>
      //   </template>
      // </el-table-column> -->
      
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 题目详情查看 -->
    <el-dialog title="题目详情" v-model="openQuestion" width="500px" append-to-body>
  <el-scrollbar>
    <el-form :model="dateById" label-position="top" label-width="400px">
      <el-form-item v-for="(item, index) in dateById" :key="index">
        <div width="400px">
          <strong>题目简介:</strong> {{ item.topic }}
        </div>
      </el-form-item>
      <el-form-item v-for="(item, index) in dateById" :key="index" >
        <div width="400px">
          <strong>题目:</strong> {{ item.questionText }}
        </div>
      </el-form-item>
        <el-form-item v-for="(item, index) in dateById" :key="index">
        <div width="400px">
          <strong>正确答案:</strong> {{ item.answer }}
        </div>
      </el-form-item>
        <el-form-item v-for="(item, index) in dateById" :key="index" >
        <div width="400px">
          <strong>难度:</strong> <dict-tag :options="question_difficulty" :value="item.difficultyLevel"/>
        </div>
      </el-form-item>
        <el-form-item v-for="(item, index) in dateById" :key="index" >
        <div width="400px">
          <strong>题目类型:</strong> <dict-tag :options="question_type" :value="item.questionType"/>
        </div>
      </el-form-item>
        
        <el-form-item v-for="(item, index) in dateById" :key="index" >
        <div width="400px">
          <strong>题库:</strong>
          <span v-for="bank in questionbankList" :key="bank.id">
            <span v-if="bank.id === item.questionBankId">{{ bank.title }}</span>
          </span>
        </div>
      </el-form-item>
        
        <el-form-item v-for="(item, index) in dateById" :key="index" >
        <div width="400px">
          <strong>出题人:</strong> {{ item.userId }}
        </div>
      </el-form-item>
        
        <el-form-item v-for="(item, index) in dateById" :key="index" >
        <div width="400px">
          <strong>创建时间:</strong> {{ parseTime(item.creatTime, '{y}-{m}-{d}') }}
        </div>
      </el-form-item>
        
        <el-form-item v-for="(item, index) in dateById" :key="index" >
        <div width="400px">
          <strong>更新时间:</strong> {{ parseTime(item.updataTime, '{y}-{m}-{d}') }}
        </div>
      </el-form-item>
    </el-form>
  </el-scrollbar>
</el-dialog>



    <!-- <el-dialog title="题目详情" v-model="openQuestion" width="700px" append-to-body>
  <el-scrollbar>
    <el-form :model="dateById" label-position="top" label-width="120px">
      <el-form-item v-for="(item, index) in dateById" :key="index" :label="'题目 ' + (index + 1)">
        <div>题目简介: {{ item.topic }}</div>
        <div>题目: {{ item.questionText }}</div>
        <div>题目的正确答案: {{ item.answer }}</div>
        <div>题目难度:
          <dict-tag :options="question_difficulty" :value="item.difficultyLevel"/>
        </div>
        <div>题目类型:
          <dict-tag :options="question_type" :value="item.questionType"/>
        </div>
        <div>题目题库:
          <span v-for="item in questionbankList" :key="item.id">
            <span v-if="item.id === item.questionBankId">
              {{ item.title }}
            </span>
          </span>
        </div>
        <div>出题人: {{ item.userId }}</div>
        <div>创建时间: 
          <span>{{ parseTime(item.creatTime, '{y}-{m}-{d}') }}</span>
        </div>
        <div>更新时间: 
          <span>{{ parseTime(item.updataTime, '{y}-{m}-{d}') }}</span>
        </div>
      </el-form-item>
    </el-form>
  </el-scrollbar>
</el-dialog> -->

    <!-- <el-dialog title="题目详情" v-model="openQuestion" width="700px" append-to-body>
    <el-table :data="dateById" style="width: 100%;">
      <el-table-column label="序号" type="index" align="center" width="100px" />
      <el-table-column label="题目简介" align="center" prop="topic" show-overflow-tooltip="true" />
      <el-table-column label="题目" align="center" prop="questionText" />
      <el-table-column label="题目的正确答案" align="center" prop="answer" />
      <el-table-column label="题目难度" align="center" prop="difficultyLevel">
        <template #default="scope">
          <dict-tag :options="question_difficulty" :value="scope.row.difficultyLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="题目类型" align="center" prop="questionType">
        <template #default="scope">
          <dict-tag :options="question_type" :value="scope.row.questionType"/>
        </template>
      </el-table-column>
      <el-table-column label="题目题库" align="center" prop="questionBankId">
        <template #default="scope">
          <template v-for="item in questionbankList" :key="item.id">
            <span v-if="item.id === scope.row.questionBankId">{{ item.title }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="出题人" align="center" prop="userId" />
      <el-table-column label="创建时间" align="center" prop="creatTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updataTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updataTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog> -->
  </div>
</template>

<script setup name="Question">
import { listQuestion, getQuestion, delQuestion, addQuestion, updateQuestion,listQuestionById } from "@/api/manage/question";
import {listQuestionbank} from "@/api/manage/questionbank";
const { proxy } = getCurrentInstance();
const { question_type, question_difficulty } = proxy.useDict('question_type', 'question_difficulty');

const questionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    questionText: null,
    answer: null,
    difficultyLevel: null,
    questionType: null,
    questionBankId: null,
    userId: null,
    topic: null
  },
  rules: {
    questionText: [
      { required: true, message: "题目的文本内容不能为空", trigger: "blur" }
    ],
    answer: [
      { required: true, message: "题目的正确答案不能为空", trigger: "blur" }
    ],
    difficultyLevel: [
      { required: true, message: "题目的难度级别不能为空", trigger: "change" }
    ],
    questionType: [
      { required: true, message: "题目的类型,1选择题2判断题3填空题4简答题不能为空", trigger: "change" }
    ],
    questionBankId: [
      { required: true, message: "题目所属题库不能为空", trigger: "change" }
    ],
    userId: [
      { required: true, message: "出题人不能为空", trigger: "change" }
    ],
    topic: [
      { required: true, message: "题目不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询题目展示列表 */
function getList() {
  loading.value = true;
  listQuestion(queryParams.value).then(response => {
    questionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    questionText: null,
    answer: null,
    difficultyLevel: null,
    questionType: null,
    questionBankId: null,
    userId: null,
    creatTime: null,
    updataTime: null,
    isDelete: null,
    topic: null
  };
  proxy.resetForm("questionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加题目展示";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getQuestion(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改题目展示";
  });
}

/**查看题目详情 */
const openQuestion = ref(false);
const dateById  = ref([]);
function handleselect(row){
  reset();
  const _id = row.id || ids.value
  listQuestionById(_id).then(response => {
    dateById.value = response.data;
    openQuestion.value = true;
    title.value = "题目详情";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["questionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateQuestion(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addQuestion(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除题目展示编号为"' + _ids + '"的数据项？').then(function() {
    return delQuestion(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/question/export', {
    ...queryParams.value
  }, `question_${new Date().getTime()}.xlsx`)
}
/**打开页面时获取题目所属题库列表 */
const questionbankList = ref([]);
function getQuestionbankList(){
  listQuestionbank({}).then(response => {
    questionbankList.value = response.rows;
  });
}

getQuestionbankList();
getList();
</script>
